﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class CountTime : MonoBehaviour
{
    public GameObject element;
    public Transform sec_1;
    public Transform sec_2;
    List<TextMesh> list = new List<TextMesh>();

    List<Transform> s_list = new List<Transform>();//圈
    List<Vector3> part_1 = new List<Vector3>();//四分之一圈
    List<Vector3> part_2 = new List<Vector3>();//二分之一圈
    int index = 0;
    float timer = 0;

    void Start()
    {
        InitSec();
        InitCricle();
        InvokeRepeating("ChangeTimeMeshColor", 0, 1);
    }

    private void Update()
    {
        //for (int i = 0; i < 60; i++)
        //{
        //    s_list[i].LookAt(sec_2);
        //}
    }

    private void InitSec()
    {
        for (int i = 1, j = 0, k = 0; i <= 60; i++, k++)
        {
            GameObject game = Instantiate(element, sec_1);
            game.name = i.ToString();
            game.transform.GetChild(0).GetComponent<TextMesh>().text = i.ToString();

            if ((i - 1) % 10 == 0)
            {
                j++;
                k = 0;
            }
            game.transform.position += new Vector3(0.25f * k, -0.25f * j, 0);
            list.Add(game.transform.GetChild(0).GetComponent<TextMesh>());
        }
        list[0].color = Color.yellow;
    }

    void ChangeTimeMeshColor()
    {
        index++;
        list[index - 1].color = Color.white;
        if (index >= 60)
        {
            index = 0;
        }
        list[index].color = Color.yellow;
        sec_2.transform.eulerAngles += new Vector3(0, 0, 90f / 15);
        foreach (var item in s_list)
        {
            item.LookAt(sec_2);
            if (item.eulerAngles.y <180)
            {
                item.eulerAngles += new Vector3(0, 0, 180);
            }
        }
        //s_list[index].LookAt(sec_2);
    }

    void InitCricle()
    {
        if (sec_2.childCount >= 60) return;
        for (int i = 0; i < 60; i++)
        {
            GameObject game = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            game.transform.parent = sec_2;
            Destroy(game.GetComponent<SphereCollider>());
            Destroy(game.GetComponent<MeshRenderer>());
            Destroy(game.GetComponent<MeshFilter>());
            //game.transform.LookAt(sec_2);
            game.name = (i + 1).ToString();
            game.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
            game.transform.position = new Vector3(0, 0.25f / 2 / Mathf.Sin(90f / 15 / 2 * Mathf.Deg2Rad), 0);//每个元素0.25距离，90度内分布15个元素
            if (!s_list.Contains(game.transform)) s_list.Add(game.transform);
        }

        for (int i = 0; i < 15; i++)//四分之一圆
        {
            s_list[i].transform.position = new Vector3(
                0.25f / 2 / Mathf.Sin(90f / 15 / 2 * Mathf.Deg2Rad) * Mathf.Sin(90f / 15 * (i + 1) * Mathf.Deg2Rad),
                0.25f / 2 / Mathf.Sin(90f / 15 / 2 * Mathf.Deg2Rad) * Mathf.Cos(90f / 15 * (i + 1) * Mathf.Deg2Rad),
                0);

            if (!part_1.Contains(s_list[i].transform.position)) part_1.Add(s_list[i].transform.position);
            if (!part_2.Contains(s_list[i].transform.position)) part_2.Add(s_list[i].transform.position);
        }
        //print(part_1.Count);
        for (int i = 15, j = 13; i < 29; i++, j--)//镜像四分之一成二分之一
        {
            s_list[i].transform.position = new Vector3(part_1[j].x, -part_1[j].y, 0);
            if (!part_2.Contains(s_list[i].transform.position)) part_2.Add(s_list[i].transform.position);
        }
        s_list[29].transform.position = new Vector3(0, -s_list[29].position.y, 0);
        if (!part_2.Contains(s_list[29].transform.position)) part_2.Add(s_list[29].transform.position);
        //print(part_2.Count+","+s_list.Count);
        for (int i = 30, j = 28; i < 59; i++, j--)//镜像二分之一成圆
        {
            s_list[i].transform.position = new Vector3(-part_2[j].x, part_2[j].y, 0);
        }
      
        sec_2.transform.eulerAngles = -Vector3.forward+ new Vector3(0, 0, 90f / 15);
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(10,10,200,50),"定界罗盘"))
        {
            for (int i = 0; i < 60; i++)
            {
                list[i].transform.parent = s_list[i];
                list[i].transform.DOLocalMove(Vector3.zero,3);
                list[i].transform.DOLocalRotate(new Vector3(-180,-90,90), 3);
            }
        }
    }
}
